﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UFIDA.U8.YYNBJY.Operate
{
    public class TBRDRECORD11
    {
        public static string RDRECORD11(string filename, string dbname, string cvouchid, bool ilog, bool iilog)
        {
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename + "]方法START======================", true, ilog, iilog, System.Drawing.Color.Black);
            try
            {
                string condition = " and 1=1 ";
                if (!string.IsNullOrEmpty(cvouchid))
                {
                    condition += " and CCODE='" + cvouchid + "' ";
                }
                string sql = @" select top 10 ID,ccode from " + dbname + @"..rdrecord11 
                                where 1 = 1 and ddate>='2024-12-01' and cwhcode='11' and ISNULL(cHandler,'')<> '' and ID not in (select MODID from EF_LF_MESU8..EF_RDRECORD11 where DBNAME = '" + dbname + @"')" + condition + " order by ID";
                DataTable dt = DBHelp.SQLHelper.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    JSONDATA.RDRECORD11 jsm = new JSONDATA.RDRECORD11();
                    jsm.GUID = Guid.NewGuid().ToString();
                    jsm.TRXID = "MaterialIssue";
                    JSONDATA.DATAItemR jdp = new JSONDATA.DATAItemR();
                    List<JSONDATA.DATAItemR> lid = new List<JSONDATA.DATAItemR>();
                    List<JSONDATA.ITEMDATAItemR> lit = new List<JSONDATA.ITEMDATAItemR>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        jdp = new JSONDATA.DATAItemR();
                        string ccode = dt.Rows[i]["ccode"].ToString();
                        string ID = dt.Rows[i]["ID"].ToString();
                        if (!string.IsNullOrEmpty(ID))
                        {
                            lit = new List<JSONDATA.ITEMDATAItemR>();
                            sql = @"select cinvcode, cBatch,ID,sum(isnull(iQuantity,0)) iQuantity from " + dbname + @".. rdrecords11 where ID='" + ID + @"' group by cinvcode, cBatch,ID ";
                            DataTable ds = DBHelp.SQLHelper.GetDataTable(sql);
                            for (int j = 0; j < ds.Rows.Count; j++)
                            {
                                JSONDATA.ITEMDATAItemR jit = new JSONDATA.ITEMDATAItemR();
                                jit.ITEM_ID = ds.Rows[j]["cinvcode"].ToString();
                                jit.LOT_ID = ds.Rows[j]["cBatch"].ToString();
                                jit.QTY = ds.Rows[j]["iQuantity"].ToString();
                                lit.Add(jit);
                            }
                            jdp.MTL_SHEET_ID = ccode;
                            jdp.ITEMDATA = lit;
                        }
                        else
                        {
                            PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]单号[" + ccode + "]没有查到明细行", true, ilog, iilog, System.Drawing.Color.Red);
                            continue;
                        }
                        lid.Add(jdp);
                        jsm.DATA = lid;
                    }
                    string json = JsonConvert.SerializeObject(jsm);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]调用[" + filename + "]参数:" + json, false, ilog, iilog, System.Drawing.Color.Red);
                    string result = PUBMethod.PMethod.CreatePostHttpResponses(Model.PUBCLASS.MESURL, json, null);
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]返回参数:" + result, false, ilog, iilog, System.Drawing.Color.Red);
                    if (!string.IsNullOrEmpty(result))
                    {
                        if (result.Substring(0, 5) != "false")
                        {
                            JSONDATA.RESJSON jre = JsonConvert.DeserializeObject<JSONDATA.RESJSON>(result);
                            string GUID = jre.GUID;
                            string RTNCODE = jre.RTNCODE;
                            string RTNMSG = jre.RTNMSG;
                            if (RTNCODE == "0")
                            {
                                List<string> lis = new List<string>();
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]同步成功:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Green);
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    string ccode = dt.Rows[i]["ccode"].ToString();
                                    string ID = dt.Rows[i]["ID"].ToString();
                                    lis.Add("insert into EF_LF_MESU8..EF_RDRECORD11(DBNAME,MOCODE,MODID,CREATETIME,DEFINE1)values('" + dbname + "','" + ccode + "','" + ID + "',GETDATE(),'" + GUID + "') ");
                                }
                                DBHelp.SQLHelper.ExecuteSqlTran(lis);
                            }
                            else
                            {
                                PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]同步失败:" + RTNMSG, true, ilog, iilog, System.Drawing.Color.Red);
                            }
                        }
                        else
                        {
                            PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]接口返回异常:" + result, true, ilog, iilog, System.Drawing.Color.Red);
                        }
                    }
                    else
                    {
                        PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "][" + filename + "]接口返回空", true, ilog, iilog, System.Drawing.Color.Red);
                    }

                }
                else
                {
                    PUBMethod.PMethod.printmsgs(filename, "[" + dbname + "]没有查到需要同步的[" + filename + "]", true, ilog, iilog, System.Drawing.Color.Red);
                }
            }
            catch (Exception ex)
            {
                PUBMethod.PMethod.printmsgs(filename, "["+dbname+"][" + filename + "]同步异常:" + ex.Message, true, ilog, true, System.Drawing.Color.Red);
            }
            PUBMethod.PMethod.printmsgs(filename, "======================[" + DateTime.Now.ToString() + "][" + dbname + "][" + filename + "]方法E N D======================", true, ilog, iilog, System.Drawing.Color.Black);
            return "";
        }

    }
}
